package com.py.payment.nettys.server;

import com.py.payment.nettys.handle.NettyServerHandler;
import com.py.payment.nettys.msg.MyDecoder;
import com.py.payment.nettys.msg.MyEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.TimeUnit;

public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 接收消息格式,自定义解析数据格式
        pipeline.addLast("decoder", new MyDecoder());
        // 发送消息格式, 自定义解析数据格式
        pipeline.addLast("encoder", new MyEncoder());
        // 这里为长时间心跳监测无数据则主动断开连接
//        pipeline.addLast(new IdleStateHandler(300, 0,0, TimeUnit.SECONDS));
        // 自定义的空闲监测
        pipeline.addLast(new NettyServerHandler());
    }
}
